#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 09-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

using Thn;
#endregion

namespace Thn.Interface.Vcl
{
    /// <summary>
    /// Wrapper for Windows Forms' TextBox in order to preserve many textbox related functionalities.
    /// </summary>
    [ToolboxItem(false)]
#if DEBUG
    public
#else
    public
#endif
 class NativeWindowsTextBox : System.Windows.Forms.TextBox
    {
        #region Fields
        /// <summary>
        /// Whether this control allows default context menu of system.
        /// </summary>
        internal bool AllowContextMenu = false;
        #endregion

        #region Properties
        #endregion

        #region Process Dialog Key
        protected override bool ProcessDialogKey(Keys keyData)
        {
            bool handled = false;
            if (OnProcessDialogKey != null) handled = OnProcessDialogKey(this, keyData);
            else
            {
                if (Parent is EditBox) ((EditBox)Parent).NotifyInnerTextBoxProcessDialogKey(keyData);
            }

            if (handled) return true;
            else return base.ProcessDialogKey(keyData);
        }
        #endregion

        #region Process Cmd Key
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            //Thn.Application.Log.Information(string.Format("Native Textbox Cmd - {0}", keyData));
            bool handled = false;
            if (OnProcessCmdKey != null) handled = OnProcessCmdKey(this, ref msg, keyData);
            else
            {
                if (Parent is EditBox)
                {
                    ((EditBox)Parent).NotifyProcessCmdKey(ref msg, keyData);
                }
            }

            if (handled) return true;
            else return base.ProcessCmdKey(ref msg, keyData);
        }
        #endregion

        #region On Key Press
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (Parent is EditBox)
            {
                ((EditBox)Parent).NotifyInnerTextBoxOnKeyPress(e);
            }
            base.OnKeyPress(e);
        }
        #endregion

        #region On Key Down
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (Parent is EditBox)
            {
                ((EditBox)Parent).NotifyInnerTextBoxOnKeyDown(e);
            }
            base.OnKeyDown(e);
        }
        #endregion

        #region On Key Up
        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (Parent is EditBox)
            {
                ((EditBox)Parent).NotifyInnerTextBoxOnKeyUp(e);
            }
            base.OnKeyUp(e);
        }
        #endregion

        #region On Lost Focus
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);

            if (Parent is EditBox)
            {
                ((EditBox)Parent).NotifyInnerTextBoxOnLostFocus(e);
            }
        }
        #endregion

        #region On Text Changed
        protected override void OnTextChanged(EventArgs e)
        {
            if (Parent is EditBox)
            {
                ((EditBox)Parent).NotifyInnerTextBoxOnTextChanged();
            }
            base.OnTextChanged(e);
        }
        #endregion

        #region On Mouse Enter
        /// <summary>
        /// Override this method for notify to the parent control that mouse pointer is enter it.
        /// </summary>
        protected override void OnMouseEnter(EventArgs e)
        {
            if (Parent != null && Parent is EditBox)
            {
                ((EditBox)Parent).NotifyInnerTextBoxOnMouseEnter(e);
            }
            #region Defensive tracing
            else
            {
                // The parent is not assigned, do nothing.
            }
            #endregion

            base.OnMouseEnter(e);
        }
        #endregion

        #region On Mouse Leave
        /// <summary>
        /// Override this method for notify to the parent control that mouse pointer is out of it.
        /// </summary>
        protected override void OnMouseLeave(EventArgs e)
        {
            if (Parent != null && Parent is EditBox)
            {
                ((EditBox)Parent).NotifyInnerTextBoxOnMouseLeave(e);
            }
            #region Defensive tracing
            else
            {
                // The parent is not assigned, do nothing.
            }
            #endregion

            base.OnMouseLeave(e);
        }
        #endregion

        #region On Mouse Down
        /// <summary>
        /// Override this method for notify to the parent control that the user is click mouse on the control.
        /// </summary>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (Parent != null && Parent is EditBox)
            {
                ((EditBox)Parent).NotifyInnerTextBoxOnMouseDown(e);
            }
            #region Defensive tracing
            else
            {
                // The parent is not assigned, do nothing.
            }
            #endregion

            base.OnMouseDown(e);
        }
        #endregion

        #region Exposed Events
        public event ProcessDialogKeyHandler OnProcessDialogKey;
        public event ProcessCmdKeyHandler OnProcessCmdKey;
        #endregion

        #region Disable Context Menu
        /// <summary>
        /// No Context Menu TextBox.
        /// </summary>
        protected override void WndProc(ref Message m)
        {
            const int WM_CONTEXTMENU = 0x7b;
            const int WM_PASTE = 0x0302;

            if (AllowContextMenu == false)
            {
                if ((m.Msg != WM_CONTEXTMENU) &&
                    (m.Msg != WM_PASTE))
                {
                    base.WndProc(ref m);
                }
                #region Defensive tracing
                else
                {
                    // do nothing in this case.
                }
                #endregion
            }
            #region Defensive tracing
            else
            {
                //proceed normally
                base.WndProc(ref m);
            }
            #endregion
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public NativeWindowsTextBox()
        { 
            
        }
        #endregion
    }

    /// <summary>
    /// for intercepting ProcessDialogKey event
    /// </summary>
    /// <returns>true means this control should continue to ProcessDialogKey
    ///          false to completely prevent this control from processing dialogkey
    /// </returns>
    public delegate bool ProcessDialogKeyHandler(object sender, Keys keyData);

    public delegate bool ProcessCmdKeyHandler(object sender, ref Message msg, Keys keyData);
}
